package com.chyb.cloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class testSentinelResourceController {

    @SentinelResource(value = "testAnn",blockHandler = "block",fallback = "testFallback")
    @GetMapping("/testH/{id}")
    public String testH(@PathVariable("id") Integer id) {
        if (id == 0) {
            throw new RuntimeException("id 不可以为零");
        }
        return "我是testH，id = " + id;
    }
    /*熔断之后*/
    public String block(@PathVariable("id") Integer id, BlockException ex) {
        return "服务启动失败，已进入熔断，id:"+id;
    }

    public String testFallback(@PathVariable("id") Integer id, Throwable ex) {
        return "程序异常：" + ex.getMessage();
    }

}
